package spring.cloud.tasks.executor.utils;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Setter
@Getter
public class SystemEnvProperties {
    public static final String NAME_CONSOLE_URI = "CONSOLE_URI";
    public static final String NAME_ZK_CLIENT_SESSION_TIMEOUT_IN_SECONDS = "ZK_CLIENT_SESSION_TIMEOUT";
    public static final String NAME_ZK_CLIENT_CONNECTION_TIMEOUT_IN_SECONDS = "K_CLIENT_CONNECTION_TIMEOUT";

    public static final String NAME_ZK_CLIENT_RETRY_TIMES = "ZK_CLIENT_RETRY_TIMES";

    private static final String NAME_EXECUTOR_CLEAN = "EXECUTOR_CLEAN";
    private static final String NAME_SHUTDOWN_TIMEOUT = "SHUTDOWN_TIMEOUT";

    /**
     * Executor离线时，其zk节点信息是否被清理
     */
    public static boolean EXECUTOR_CLEAN = Boolean.parseBoolean(System.getProperty(NAME_EXECUTOR_CLEAN, System.getenv(NAME_EXECUTOR_CLEAN)));

    /**
     * Executor优雅退出的全局默认超时时间（单位：精确到秒，默认1分钟）
     */
    public static long SHUTDOWN_TIMEOUT = 60;
    public static int SHUTDOWN_TIMEOUT_MAX = 5 * 60 - 10;
    public static String CONSOLE_URI = trim(System.getProperty(NAME_CONSOLE_URI, System.getenv(NAME_CONSOLE_URI)));
    public static List<String> CONSOLE_URI_LIST = new ArrayList<>();
    public static int ZK_CLIENT_SESSION_TIMEOUT_IN_SECONDS = -1;
    public static int ZK_CLIENT_CONNECTION_TIMEOUT_IN_SECONDS = -1;
    public static int ZK_CLIENT_RETRY_TIMES = -1;

    static {
        loadProperties();
    }

    public static void loadProperties() {


        String shutdownTimeout = System.getProperty(NAME_SHUTDOWN_TIMEOUT, System.getenv(NAME_SHUTDOWN_TIMEOUT));
        if (shutdownTimeout != null) {
            try {
                SHUTDOWN_TIMEOUT = Integer.parseInt(shutdownTimeout);
            } catch (Throwable t) {
                LogUtils.error(log, "", t.getMessage(), t);
            }
        }
        if (SHUTDOWN_TIMEOUT > SHUTDOWN_TIMEOUT_MAX) {
            SHUTDOWN_TIMEOUT = SHUTDOWN_TIMEOUT_MAX;
        }


        String zkClientSessionTimeoutStr = System.getProperty(NAME_ZK_CLIENT_SESSION_TIMEOUT_IN_SECONDS, System.getenv(NAME_ZK_CLIENT_SESSION_TIMEOUT_IN_SECONDS));
        if (zkClientSessionTimeoutStr != null) {
            try {
                ZK_CLIENT_SESSION_TIMEOUT_IN_SECONDS = Integer.parseInt(zkClientSessionTimeoutStr);
            } catch (Throwable t) {
                LogUtils.error(log, "", t.getMessage(), t);
            }
        }

        String zkClientConnectionTimeoutStr = System.getProperty(NAME_ZK_CLIENT_CONNECTION_TIMEOUT_IN_SECONDS, System.getenv(NAME_ZK_CLIENT_CONNECTION_TIMEOUT_IN_SECONDS));
        if (zkClientConnectionTimeoutStr != null) {
            try {
                ZK_CLIENT_CONNECTION_TIMEOUT_IN_SECONDS = Integer.parseInt(zkClientConnectionTimeoutStr);
            } catch (Throwable t) {
                LogUtils.error(log, "", t.getMessage(), t);
            }
        }

        String zkClientRetryTimes = System.getProperty(NAME_ZK_CLIENT_RETRY_TIMES, System.getenv(NAME_ZK_CLIENT_RETRY_TIMES));
        if (zkClientRetryTimes != null) {
            try {
                ZK_CLIENT_RETRY_TIMES = Integer.parseInt(zkClientRetryTimes);
            } catch (Throwable t) {
                LogUtils.error(log, "", t.getMessage(), t);
            }
        }


    }

    protected static String trim(String property) {
        if (property != null && property.length() > 0) {
            return property.trim();
        }
        return property;
    }

    public static void init() {
        if (CONSOLE_URI != null) {
            String[] split = CONSOLE_URI.split(",");
            if (split != null) {
                for (String tmp : split) {
                    tmp = tmp.trim();
                    if (!tmp.isEmpty()) {
                        CONSOLE_URI_LIST.add(tmp);
                    }
                }
            }
        }
    }

}
